$(function(){
    //轮播
    $('.pagination-box').on('click','.pagination-unit',function(){
        $(this).addClass('pagination-unit-active').siblings('.pagination-unit').removeClass('pagination-unit-active')
        var m =$(this).index()/4*100 + '%';
        $('.slider-container').css({
            transform:'translateX(-'+m +')'
        })
    })

//    律师提现申请开始
    //提现时间
    $('#withdraw-cash-start').on('focus',function(){
        WdatePicker()
    })

    var d = new Date();

    new YMDselect('year1','month1','day1',d.getFullYear(),d.getMonth()+1,d.getDate());
    new YMDselect('year2','month2','day2',d.getFullYear(),d.getMonth()+1,d.getDate());

    /**
     * 初始化图表
     */
  //  init_chart_data();

    //加载增量用户数据
    load_user_charts();

    //加载电话数据
    load_call_charts();

})



var options = {

    //Boolean - If we show the scale above the chart data
    scaleOverlay : false,

    //Boolean - If we want to override with a hard coded scale
    scaleOverride : false,

    //** Required if scaleOverride is true **
    //Number - The number of steps in a hard coded scale
    scaleSteps : null,
    //Number - The value jump in the hard coded scale
    scaleStepWidth : 5,
    //Number - The scale starting value
    scaleStartValue : null,

    //String - Colour of the scale line
    scaleLineColor : "rgba(0,0,0,.1)",

    //Number - Pixel width of the scale line
    scaleLineWidth : 1,

    //Boolean - Whether to show labels on the scale
    scaleShowLabels : false,

    //Interpolated JS string - can access value
    scaleLabel : "<%=value%>",

    //String - Scale label font declaration for the scale label
    scaleFontFamily : "'Arial'",

    //Number - Scale label font size in pixels
    scaleFontSize : 12,

    //String - Scale label font weight style
    scaleFontStyle : "normal",

    //String - Scale label font colour
    scaleFontColor : "#666",

    ///Boolean - Whether grid lines are shown across the chart
    scaleShowGridLines : true,

    //String - Colour of the grid lines
    scaleGridLineColor : "rgba(0,0,0,.05)",

    //Number - Width of the grid lines
    scaleGridLineWidth : 1,

    //Boolean - Whether the line is curved between points
    bezierCurve : true,

    //Boolean - Whether to show a dot for each point
    pointDot : true,

    //Number - Radius of each point dot in pixels
    pointDotRadius : 3,

    //Number - Pixel width of point dot stroke
    pointDotStrokeWidth : 1,

    //Boolean - Whether to show a stroke for datasets
    datasetStroke : true,

    //Number - Pixel width of dataset stroke
    datasetStrokeWidth : 1,

    //Boolean - Whether to fill the dataset with a colour
    datasetFill : true,

    //Boolean - Whether to animate the chart
    animation : true,

    //Number - Number of animation steps
    animationSteps : 60,

    //String - Animation easing effect
    animationEasing : "easeOutQuart",

    //Function - Fires when the animation is complete
    onAnimationComplete : null

};


var addChart;

var regiserTime="";

/**
 * 加载用户增量记录
 */
function load_user_charts(){

    Loading.show();

    $.ajax({
        type:"get",
        url:"/sysuser/getUserChart",
        data:{regiserTime:regiserTime},
        dataType: "json",
        success:function(data){


              switch (regiserTime.length){

                  case 0:

                  case 10:

                      init_chart_data_hours(24,data.data);

                  break;


                  case 7:


                      var years=regiserTime.substring(0,3);

                      var month=regiserTime.substring(6);

                      var days=getDaysInOneMonth(years,month);

                      init_chart_data_hours(days,data.data);

                      break;

                  case 4:

                      init_chart_data_hours(12,data.data);

                      break;


              }


            Loading.hide();

        },
        error:function(){
            alert("报错啦");
        }
    })


}



function getDaysInOneMonth(year, month){
    month = parseInt(month, 10);
    var d= new Date(year, month, 0);
    return d.getDate();
}


function init_chart_data_hours(num,chartData){

    var data_line_chart = {
        labels : [],
        datasets : [
            {
                fillColor : "rgba(220,220,220,0.5)",
                strokeColor : "rgba(220,220,220,1)",
                pointColor : "rgba(220,220,220,1)",
                pointStrokeColor : "#fff",
                data : []
            }
        ]
    }


    var danwei="";

    if(num==24){
        danwei="时";
    }else if(num=="12"){
        danwei="月";
    }else{
        danwei="日";
    }

    for(var i=1;i<=num;i++){

        data_line_chart.labels.push(i+danwei);

          var data_num=0;

          for(k in chartData){

               if(chartData[k].h==i){

                   data_num=chartData[k].c;

                   break;

               }

          }

        data_line_chart.datasets[0].data.push(data_num);

       }


        if(addChart!=undefined){
          addChart.destroy();
        }

        var canvas=document.getElementById("myChart");

        canvas.width  = window.innerWidth*0.93;

        canvas.height  = window.innerHeight*0.5;
     
        var ctx = canvas.getContext("2d");

       addChart=new Chart(ctx).Line(data_line_chart,options);


}







var addCallChart;

var createTime="";

/**
 * 加载电话记录
 */
function load_call_charts(){

    Loading.show();

    $.ajax({
        type:"get",
        url:"/call/getCallChart",
        data:{createTime:createTime},
        dataType: "json",
        success:function(data){


            switch (createTime.length){

                case 0:

                case 10:

                    init_chart_call_hours(24,data.data);

                    break;


                case 7:


                    var years=createTime.substring(0,3);

                    var month=createTime.substring(6);

                    var days=getDaysInOneMonth(years,month);

                    init_chart_call_hours(days,data.data);

                    break;

                case 4:

                    init_chart_call_hours(12,data.data);

                    break;


            }


            Loading.hide();

        },
        error:function(){
            alert("报错啦");
        }
    })


}



function init_chart_call_hours(num,chartData){

    var data_line_chart = {
        labels : [],
        datasets : [
            {
                fillColor : "rgba(255,0,0,0.5)",
                strokeColor : "rgba(255,0,0,1)",
                pointColor : "rgba(255,0,0,1)",
                pointStrokeColor : "#fff",
                data : []
            },
            {
                fillColor : "rgba(151,187,205,0.5)",
                strokeColor : "rgba(151,187,205,1)",
                pointColor : "rgba(151,187,205,1)",
                pointStrokeColor : "#fff",
                data : []
            }
        ]
    }


    var danwei="";

    if(num==24){
        danwei="时";
    }else if(num=="12"){
        danwei="月";
    }else{
        danwei="日";
    }

    for(var i=1;i<=num;i++){

        data_line_chart.labels.push(i+danwei);

       // alert(JSON.stringify(chartData));

        var data_num=0;

        for(k in chartData){


            var v=chartData[k];

           // alert(JSON.stringify(v));

            for(j in v){

             //   alert(JSON.stringify(v[j]));

            if(v[j].h==i){

                data_num=v[j].c;

                break;

            }


            }

            data_line_chart.datasets[k].data.push(data_num);

        }


    }


    if(addCallChart!=undefined){
        addCallChart.destroy();
    }

    var canvas=document.getElementById("myChart2");

    canvas.width  = window.innerWidth*0.93;

    canvas.height  = window.innerHeight*0.5;

    var ctx = canvas.getContext("2d");

    addCallChart=new Chart(ctx).Line(data_line_chart,options);


}



/**
 * 搜索增量时间
 */
function select_add_chart(){

    var year=$("select[name='year1'] option:selected").val();
    var month=$("select[name='month1'] option:selected").val();
    var day=$("select[name='day1'] option:selected").val();

    regiserTime="";

    if(year!="0"){

        regiserTime=year;
    }

    if(month!="0"){
        regiserTime+="-";
        month.length==1?regiserTime+='0':'';
        regiserTime+=month;
    }

    if(day!="0"){
        regiserTime+="-";
        day.length==1?regiserTime+='0':'';
        regiserTime+=day;
    }

    load_user_charts();

}


/**
 * 搜索电话日志时间
 */
function select_call_chart(){

    var year=$("select[name='year2'] option:selected").val();
    var month=$("select[name='month2'] option:selected").val();
    var day=$("select[name='day2'] option:selected").val();

    createTime="";

    if(year!="0"){

        createTime=year;
    }

    if(month!="0"){
        createTime+="-";
        month.length==1?createTime+='0':'';
        createTime+=month;
    }

    if(day!="0"){
        createTime+="-";
        day.length==1?createTime+='0':'';
        createTime+=day;
    }

    load_call_charts();

}